﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using BPM.Base.Tools;
using System.ServiceModel.Description;
using System.Xml;

namespace ESOL.Services
{
    public class WCFService
    {
        private ServiceHost host;

        private Uri serverUrl;

        public WCFService(Type serviceType,Uri serverUrl,Type contractType)
        {
            try
            {
                this.serverUrl = serverUrl;

                host = new ServiceHost(serviceType, serverUrl);

                host.Closed += new EventHandler(host_Closed);
                host.Opened += new EventHandler(host_Opened);
                host.UnknownMessageReceived += new EventHandler<UnknownMessageReceivedEventArgs>(host_UnknownMessageReceived);
                host.Faulted += new EventHandler(host_Faulted);

                ServiceMetadataBehavior behavior = new ServiceMetadataBehavior();

                behavior.HttpGetEnabled = true;
                host.Description.Behaviors.Add(behavior);

                System.ServiceModel.BasicHttpBinding bh = new BasicHttpBinding();

                XmlDictionaryReaderQuotas quotas = new XmlDictionaryReaderQuotas();
                quotas.MaxStringContentLength = 7000000;
                quotas.MaxArrayLength = 2147483647;
                bh.ReaderQuotas = quotas;

                bh.MaxReceivedMessageSize = 7000000;

                host.AddServiceEndpoint(contractType, bh, "PW/");

            }
            catch (System.Exception ex)
            {
                MyLog.writeError(ex.Message);

                throw new MyException(ex.Message);
            }
        }

        void host_Faulted(object sender, EventArgs e)
        {
            MyLog.writeError("WCF-Faulted");
        }

        void host_UnknownMessageReceived(object sender, UnknownMessageReceivedEventArgs e)
        {
            MyLog.writeError("WCF-UnknownMessageReceived");
        }

        void host_Opened(object sender, EventArgs e)
        {
            MyLog.writeInfo("WCF服务已启动,url:" + host.BaseAddresses[0].AbsoluteUri.ToString());
        }

        void host_Closed(object sender, EventArgs e)
        {
            MyLog.writeInfo("WCF服务已停止");
        }

        public int? open()
        {
            try
            {
                if (host.State == CommunicationState.Created)
                {
                    host.Open();
                    return null;
                }
                return 1;
            }
            catch (System.Exception ex)
            {

                MyLog.writeError(ex.Message);

                throw new MyException(ex.Message);
            }
        }

        public int? close()
        {
            if (host.State == CommunicationState.Opened)
            {
                host.Close();
                return null;
            }
            else
            {
                return 1;
            }
        }
    }
}
